package com.jindu.producer.finance.controller;

import com.jindu.kernel.page.AlphaPage;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.finance.jpa.ProductJpa;
import com.jindu.producer.finance.model.CContentEntity;
import com.jindu.producer.finance.model.FLegalEntity;
import com.jindu.producer.finance.model.FProductEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/10/18
 */
@Api(description = "众测产品管理", tags = "ProductController", basePath = "/product")
@RestController
@RequestMapping(value = "/product",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ProductController {

    @Autowired
    ProductJpa productJpa;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据众测产品的ID，查询众测产品的信息", notes = "返回单个众测产品的信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public JsonResult<FProductEntity> get(@RequestParam String id){
        return JsonResult.getSuccessResult(productJpa.findById(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据分页参数，查询众测产品的信息", notes = "返回众测产品的信息集合")
    @RequestMapping(value = "getAll",method = RequestMethod.GET)
    public JsonResult<AlphaPage<FProductEntity>> getAll(@RequestParam(value = "pageNo",defaultValue = "0") Integer pageNo,
                                                        @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "type",required = false) Integer type,
                                                        @RequestParam(value = "realm",required = false) Integer realm,
                                                        @RequestParam(value = "status",required = false) Integer status,
                                                        @RequestParam(value = "name", required = false) String name) {

        Specification querySpecifi = (Specification<FProductEntity>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if(type != null){
                predicates.add(criteriaBuilder.equal(root.get("type"),type));
            }
            if(realm != null){
                predicates.add(criteriaBuilder.equal(root.get("realm"),realm));
            }
            if(status != null){
                predicates.add(criteriaBuilder.equal(root.get("status"),status));
            }
            if(name != null){
                predicates.add(criteriaBuilder.like(root.get("name"),"%"+name+"%"));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Page<FProductEntity> entities=productJpa.findAll(querySpecifi,new PageRequest(pageNo, pageSize,sort));
        return new AlphaPage<>(entities).toJsonResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存众测产品的信息", notes = "保存众测产品的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<FProductEntity> save(@RequestBody FProductEntity productEntity){

        FProductEntity fProductEntity=productJpa.findById(productEntity.getId());

        if(fProductEntity == null){
            productEntity.setId(IdGenerator.uuid());
            productEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            productEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(productJpa.saveAndFlush(productEntity));
        }else {
            productEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(productEntity,fProductEntity, AlphaBeanUtils.getNullPropertyNames(productEntity));
            return JsonResult.getSuccessResult(productJpa.saveAndFlush(fProductEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除众测产品的信息", notes = "根据ID,删除相关的所有数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(productJpa.deleteById(id));
    }


}
